export default{
    install:function(Vue,opt){
        let _this = this;
        const KEY = Buffer.from("12345678","utf-8");
        const IV = Buffer.from("12345678","utf-8");
        // 加密
        Vue.prototype.encrypt = function(content){
        if(!content) {
        return;
        }
        //转为byte数组
        content = this.stringToByte(content);
        let CBC = this.des.CBC.instantiate(this.des.DES);
        let enc = CBC.create({
        type: 'encrypt',
        key: KEY,
        iv: IV
        });
        //加密，结果为byte[]
        let result = enc.final(content);
        //转为b64字符串
        let b64str = this.b64.fromByteArray(result);
        return b64str;
        // return content;
        }
        // 解密
        Vue.prototype.decrypt = function(content){
            if(!content) {
                return;
            }
            //b64解码
            content = this.b64.toByteArray(content);
            let CBC = this.des.CBC.instantiate(this.des.DES);
            let dec = CBC.create({
                type: 'decrypt',
                key: KEY,
                iv: IV
            });
            //解密
            content = dec.final(content);
            content = this.byteToString(content);
            return content;
        }
        Vue.prototype.stringToByte = function(str){
            var bytes = new Array();
            var len, c;
            len = str.length;
            for (var i = 0; i < len; i++) {
                c = str.charCodeAt(i);
                if (c >= 0x010000 && c <= 0x10FFFF) {
                    bytes.push(((c >> 18) & 0x07) | 0xF0);
                    bytes.push(((c >> 12) & 0x3F) | 0x80);
                    bytes.push(((c >> 6) & 0x3F) | 0x80);
                    bytes.push((c & 0x3F) | 0x80);
                } else if (c >= 0x000800 && c <= 0x00FFFF) {
                    bytes.push(((c >> 12) & 0x0F) | 0xE0);
                    bytes.push(((c >> 6) & 0x3F) | 0x80);
                    bytes.push((c & 0x3F) | 0x80);
                } else if (c >= 0x000080 && c <= 0x0007FF) {
                    bytes.push(((c >> 6) & 0x1F) | 0xC0);
                    bytes.push((c & 0x3F) | 0x80);
                } else {
                    bytes.push(c & 0xFF);
                }
            }
            return bytes;
        }

        Vue.prototype.byteToString = function(arr) {
            if (typeof arr === 'string') {
                return arr;
            }
            var str = '',
                _arr = arr;
            for (var i = 0; i < _arr.length; i++) {
                var one = _arr[i].toString(2),
                    v = one.match(/^1+?(?=0)/);
                if (v && one.length == 8) {
                    var bytesLength = v[0].length;
                    var store = _arr[i].toString(2).slice(7 - bytesLength);
                    for (var st = 1; st < bytesLength; st++) {
                        store += _arr[st + i].toString(2).slice(2);
                    }
                    str += String.fromCharCode(parseInt(store, 2));
                    i += bytesLength - 1;
                } else {
                    str += String.fromCharCode(_arr[i]);
                }
            }
            return str;
        },

        //获取档案接口
        Vue.prototype.getDocument = function(type,arr){
            let _this = this;
            let paramfetch={
                "searchMap":{
                    "base_table":type
                }
            }
            this.$fetch(this.$root.domainIp+'maintain/apporder/baselist/',{param:this.encrypt(JSON.stringify(paramfetch))})
            .then((response) => {
                // this.loading = false;
                if(response.code == 0){
                    let json_data = JSON.parse(this.decrypt(response.data));
                    json_data.data.forEach(item => {
                        arr.push(item);
                    });
                }else {
                    _this.$message({
                        message:(response.message || '请检查网络是否连接'),
                        center: true
                    });
                }
            }).catch(function (error) {
                // this.loading = false;
                console.log(error);
                _this.$message({
                    message:('请检查网络是否连接'),
                    center: true
                });
            });
        }

        
         


    }
}